package com.nstar.orderexpress.commands
{
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.nstar.base.util.Logger;
	import com.nstar.components.ErrorView;
	import com.nstar.orderexpress.business.OrderExpressDelegate;
	import com.nstar.orderexpress.control.SearchEvent;
	import com.nstar.orderexpress.model.Model;
	import com.nstar.orderexpress.vo.OrderMenuVO;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.IResponder;

	public class SearchCommand implements Command, IResponder
	{
		private var model:Model = Model.getInstance();
		private var logger:Logger = Logger.getInstance();
		private var benchmark:Date;
		
		public function execute(event:CairngormEvent):void
		{ 
			//TODO: implement function
			var delegate:OrderExpressDelegate = new OrderExpressDelegate(this);//由代理来调用异步处理方法
			//强制转换成自定义的CairngormEvent事件，以获得自定义事件的属性
			var e:SearchEvent = SearchEvent(event);
			//调用代理中的方法
			delegate.findMenus(e.request);
			if (model.isDebuging){
				result(null);	
			}
		}
		
		//正常返回的处理
		public function result(data:Object):void
		{
			//TODO: implement function
			var ary:ArrayCollection = new ArrayCollection();
			if (model.isDebuging){//调试状态
				data = new Object();
				//返回结果到前台并调整视图大小
				for(var i:int=0;i<10;i++){
					var om:OrderMenuVO = new OrderMenuVO();
					om.menuId = i.toString();
					om.name = "menu "+i;
					om.price = 50*Math.random();
					om.menuDesc = "this is "+om.name;
					ary.addItem(om);
				}
			}else{
				var mrary:ArrayCollection = new ArrayCollection(data.result.list);
				for(var i:int = 0;i<mrary.length;i++){
					ary.addItem(mrary[i].orderMenu);
				}
			}
			model.mainApp.searchResultView.atlResult.dataProvider = ary;
			model.mainApp.resizeView(null);
		}
		
		//失败返回处理
		public function fault(info:Object):void
		{
			//TODO: implement function
			ErrorView.showErrorView(info);
		}
		
		private function translate(item:Object ):Object{
			return item;
		}			
		
	}
}